<?php
// use the dPFramework to have easy database operations (store, delete etc.) by using its ObjectOrientedDesign
// therefore we have to create a child class for the module einstein

// a class named (like this) in the form: module/module.class.php is automatically loaded by the dPFramework

/**
 *	@package dotProject
 *	@subpackage modules
 *	@version $Revision: 1.0 $
*/

// include the powerful parent class that we want to extend for einstein
require_once( $AppUI->getSystemClass ('dp' ) );		// use the dPFramework for easy inclusion of this class here

/**
 * The Cost Center Class
 */
class CMdCostCenter extends CDpObject {
	// link variables to the einstein object (according to the existing columns in the database table einstein)
	var $cc_id = NULL;	//use NULL for a NEW object, so the database automatically assigns an unique id by 'NOT NULL'-functionality
	var $cc_code = NULL;
	var $cc_name = NULL;
	var $cc_division = NULL;
	var $cc_active = 1;

	// the constructor of the CEinstein class, always combined with the table name and the unique key of the table
	function CMdCostCenter() {
		$joins = array(
			array('label'=>'Customer',name=>'companies', 'idfield'=>'company_id', 'joinfield'=>'company_costcenter'),
			array('label'=>'Supplier',name=>'suppliers', 'idfield'=>'sup_id', 'joinfield'=>'sup_costcenter'),
			array('label'=>'Resource',name=>'contacts', 'idfield'=>'contact_id', 'joinfield'=>'contact_costcenter'),
			array('label'=>'Item',name=>'items', 'idfield'=>'item_id', 'joinfield'=>'item_costcenter'),
			array('label'=>'Task log',name=>'task_log', 'idfield'=>'task_log_id', 'joinfield'=>'task_log_costcenter'),
			array('label'=>'Task',name=>'tasks', 'idfield'=>'task_id', 'joinfield'=>'task_costcenter'),
			array('label'=>'Transportation ',name=>'transportations', 'idfield'=>'trans_id', 'joinfield'=>'trans_costcenter'),
		);
		$this->CDpObject( 'costcenters', 'cc_id', 'cc_code', $joins, 'masterdata' );
	}

	function check() {
		if (!isset($this->_tbl_unique) || $this->_tbl_unique == NULL) {
			return NULL;
		}
		$q = new DBQuery();
		$q->addTable("$this->_tbl");
		$q->addWhere("$this->_tbl_key != $this->cc_id");
		$q->addWhere("$this->_tbl_unique = '$this->cc_code'");
		$result = $q->loadResult();
		if (isset($result) && $result != '') {
			return 'Duplicate cost center code: ' . $this->cc_code;
		}
		return NULL;
	}

	// overload the delete method of the parent class for adaptation for einstein's needs
	function getDivisionName(){
		//$sql = "select status_name from md_status where status_id = '" . $this->unit_status . "'";
		$q  = new DBQuery;
		$q->addTable('md_division');
		$q->addQuery('div_name');
		$q->addWhere('div_id = '.$this->cc_division);
		$sql = $q->prepare();
		$q->clear();
		$div_name = db_loadResult( $sql );
		return $div_name;
 	}
	function loadAllCostCenter($active = null){
		$q = new DBQuery;
		$q->addTable('costcenters');
		$q->addJoin('divisions', '', 'div_id = cc_division');
		$q->addQuery('costcenters.*, div_name');
		if($active != null || $active !=''){
			$q->addWhere('cc_active = '.$active);
		}
		$psql = $q->prepare();
		$q->clear();
		return db_loadList($psql);
	}
}
function getCostCenterList($required=true){
	$q  = new DBQuery;
	$q->addTable('costcenters');
	$q->addJoin('divisions','','cc_division = div_id');
	$q->addQuery("cc_id,CONCAT('[',div_name,']',' ',cc_name) as cc_name");
	$q->addWhere('cc_active = 1 ');
	$results = $q->loadHashList();
	if (!$required) {
		return arrayMerge(array('' => ''), $results);
	}
	return $results;
}
?>